<?php

namespace addons\chuanglansms;

use app\common\library\Menu;
use think\Addons;

/**
 * 插件
 */
class Chuanglansms extends Addons
{

    private $appkey = null;
    private $appsecret = null;
    private $regionid = null;
    private $signname = null;
    private $config = null;
    public function ConfigInit()
    {
        $this->config = $this->getConfig();

        $this->appkey = $this->config['appkey'];
        $this->appsecret = $this->config['appsecret'];
        $this->apiUrl = $this->config['apiUrl'];
        $this->signname = $this->config['signname'];
    }
    /**
     * 短信发送通知
     * @param array $params
     * @return  boolean
     */
    public function smsSend(&$params)
    {
        $this->ConfigInit();
        //创蓝接口参数
        $postArr = array (
            //'account'  =>  $a,
            'account'  =>  $this->appkey,
            'password' => $this->appsecret,
            'msg' => '【优策宝】您好，验证码:'. $params['code'] .'，请勿告诉他人！',
            'mobile' => '0086'.$params['mobile'],
            'report' => true
        );
        //echo '<pre>';print_r($postArr);die;
        $result = $this->curlPost( $this->apiUrl, $postArr);
        $result = json_decode($result,true);
        if($result['code'] == 0){
            return true;
        }else{
            write_log($result['error'],'短信发送错误');
            return false;
        }
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     *
     */
    private function curlPost($url,$postFields){
        $postFields = json_encode($postFields);

        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );
        return $result;
    }

    /**
     * 检测验证是否正确
     * @param Sms $params
     * @return  boolean
     */
    public function smsCheck(&$params)
    {
        return true;
    }
    /**
     * 记录失败信息
     * @param [type] $err [description]
     */
    private function setError($err)
    {
        $this->sendError = $err;
    }

    /**
     * 获取失败信息
     * @return [type] [description]
     */
    public function getError()
    {
        return $this->sendError;
    }

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [
            [
                'name'   => 'Chuanglansms',
                'title'  => '创蓝短信插件',
                'ismenu' => 1,
                'icon'   => 'fa fa-list',
                'remark' => '',
            ]
        ];
        Menu::create($menu);
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('Chuanglansms');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable('Chuanglansms');
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('Chuanglansms');
    }

    /**
     * 实现钩子方法
     * @return mixed
     */
    public function testhook($param)
    {
        // 调用钩子时候的参数信息
        print_r($param);
        // 当前插件的配置信息，配置信息存在当前目录的config.php文件中，见下方
        print_r($this->getConfig());
        // 可以返回模板，模板文件默认读取的为插件目录中的文件。模板名不能为空！
        //return $this->fetch('view/info');
    }

}
